[[...path]].page.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import React, { useEffect } from 'react';
  2. import { type IPagePopulatedToShowRevision, getIdForRef } from '@growi/core';
  3. import type {
  4. GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import Head from 'next/head';
  8. import superjson from 'superjson';
  9. import { ShareLinkLayout } from '~/components-universal/Layout/ShareLinkLayout';
  10. import { DrawioViewerScript } from '~/components-universal/Script/DrawioViewerScript';
  11. import { ShareLinkPageView } from '~/components-universal/ShareLinkPageView';
  12. import GrowiContextualSubNavigationSubstance from '~/components/Navbar/GrowiContextualSubNavigation';
  13. import type { SupportedActionType } from '~/interfaces/activity';
  14. import { SupportedAction } from '~/interfaces/activity';
  15. import type { CrowiRequest } from '~/interfaces/crowi-request';
  16. import type { RendererConfig } from '~/interfaces/services/renderer';
  17. import type { IShareLinkHasId } from '~/interfaces/share-link';
  18. import type { PageDocument } from '~/server/models/page';
  19. import ShareLink from '~/server/models/share-link';
  20. import {
  21. useCurrentUser, useRendererConfig, useIsSearchPage, useCurrentPathname,
  22. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault, useIsContainerFluid, useIsEnabledMarp,
  23. } from '~/stores-universal/context';
  24. import { useCurrentPageId, useIsNotFound, useSWRMUTxCurrentPage } from '~/stores/page';
  25. import loggerFactory from '~/utils/logger';
  26. import type { NextPageWithLayout } from '../_app.page';
  27. import type { CommonProps } from '../utils/commons';
  28. import {
  29. getServerSideCommonProps, generateCustomTitleForPage, getNextI18NextConfig, skipSSR, addActivity,
  30. } from '../utils/commons';
  31. const logger = loggerFactory('growi:next-page:share');
  32. type Props = CommonProps & {
  33. shareLinkRelatedPage?: IShareLinkRelatedPage,
  34. shareLink?: IShareLinkHasId,
  35. isNotFound: boolean,
  36. isExpired: boolean,
  37. disableLinkSharing: boolean,
  38. isSearchServiceConfigured: boolean,
  39. isSearchServiceReachable: boolean,
  40. isSearchScopeChildrenAsDefault: boolean,
  41. isEnabledMarp: boolean,
  42. drawioUri: string | null,
  43. rendererConfig: RendererConfig,
  44. skipSSR: boolean,
  45. ssrMaxRevisionBodyLength: number,
  46. };
  47. type IShareLinkRelatedPage = IPagePopulatedToShowRevision & PageDocument;
  48. superjson.registerCustom<IShareLinkRelatedPage, string>(
  49. {
  50. isApplicable: (v): v is IShareLinkRelatedPage => {
  51. return v != null
  52. && v.toObject != null
  53. && v.lastUpdateUser != null
  54. && v.creator != null
  55. && v.revision != null;
  56. },
  57. serialize: (v) => { return superjson.stringify(v.toObject()) },
  58. deserialize: (v) => { return superjson.parse(v) },
  59. },
  60. 'IShareLinkRelatedPageTransformer',
  61. );
  62. // GrowiContextualSubNavigation for shared page
  63. // get page info from props not to send request 'GET /page' from client
  64. type GrowiContextualSubNavigationForSharedPageProps = {
  65. page?: IPagePopulatedToShowRevision,
  66. isLinkSharingDisabled: boolean,
  67. }
  68. const GrowiContextualSubNavigationForSharedPage = (props: GrowiContextualSubNavigationForSharedPageProps): JSX.Element => {
  69. const { page, isLinkSharingDisabled } = props;
  70. return (
  71. <GrowiContextualSubNavigationSubstance currentPage={page} isLinkSharingDisabled={isLinkSharingDisabled} />
  72. );
  73. };
  74. const SharedPage: NextPageWithLayout<Props> = (props: Props) => {
  75. useCurrentPathname(props.shareLink?.relatedPage.path);
  76. useIsSearchPage(false);
  77. useIsNotFound(props.isNotFound);
  78. useShareLinkId(props.shareLink?._id);
  79. useCurrentPageId(props.shareLink?.relatedPage._id);
  80. useCurrentUser(props.currentUser);
  81. useRendererConfig(props.rendererConfig);
  82. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  83. useIsSearchServiceReachable(props.isSearchServiceReachable);
  84. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  85. useIsEnabledMarp(props.rendererConfig.isEnabledMarp);
  86. useIsContainerFluid(props.isContainerFluid);
  87. const { trigger: mutateCurrentPage, data: currentPage } = useSWRMUTxCurrentPage();
  88. useEffect(() => {
  89. if (!props.skipSSR) {
  90. return;
  91. }
  92. if (props.shareLink?.relatedPage._id != null && !props.isNotFound) {
  93. mutateCurrentPage();
  94. }
  95. }, [mutateCurrentPage, props.isNotFound, props.shareLink?.relatedPage._id, props.skipSSR]);
  96. const pagePath = props.shareLinkRelatedPage?.path ?? '';
  97. const title = generateCustomTitleForPage(props, pagePath);
  98. return (
  99. <>
  100. <Head>
  101. <title>{title}</title>
  102. </Head>
  103. <div className="dynamic-layout-root justify-content-between">
  104. <GrowiContextualSubNavigationForSharedPage page={currentPage ?? props.shareLinkRelatedPage} isLinkSharingDisabled={props.disableLinkSharing} />
  105. <ShareLinkPageView
  106. pagePath={pagePath}
  107. rendererConfig={props.rendererConfig}
  108. page={currentPage ?? props.shareLinkRelatedPage}
  109. shareLink={props.shareLink}
  110. isExpired={props.isExpired}
  111. disableLinkSharing={props.disableLinkSharing}
  112. />
  113. </div>
  114. </>
  115. );
  116. };
  117. SharedPage.getLayout = function getLayout(page) {
  118. return (
  119. <>
  120. <DrawioViewerScript drawioUri={page.props.rendererConfig.drawioUri} />
  121. <ShareLinkLayout>{page}</ShareLinkLayout>
  122. </>
  123. );
  124. };
  125. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  126. const req: CrowiRequest = context.req as CrowiRequest;
  127. const { crowi } = req;
  128. const { configManager, searchService } = crowi;
  129. props.disableLinkSharing = configManager.getConfig('crowi', 'security:disableLinkSharing');
  130. props.isSearchServiceConfigured = searchService.isConfigured;
  131. props.isSearchServiceReachable = searchService.isReachable;
  132. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  133. props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');
  134. props.rendererConfig = {
  135. isSharedPage: true,
  136. isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  137. isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  138. isEnabledMarp: configManager.getConfig('crowi', 'customize:isEnabledMarp'),
  139. adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  140. isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  141. drawioUri: configManager.getConfig('crowi', 'app:drawioUri'),
  142. plantumlUri: configManager.getConfig('crowi', 'app:plantumlUri'),
  143. // XSS Options
  144. isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:rehypeSanitize:isEnabledPrevention'),
  145. sanitizeType: configManager.getConfig('markdown', 'markdown:rehypeSanitize:option'),
  146. customAttrWhitelist: JSON.parse(crowi.configManager.getConfig('markdown', 'markdown:rehypeSanitize:attributes')),
  147. customTagWhitelist: crowi.configManager.getConfig('markdown', 'markdown:rehypeSanitize:tagNames'),
  148. highlightJsStyleBorder: crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  149. };
  150. props.ssrMaxRevisionBodyLength = configManager.getConfig('crowi', 'app:ssrMaxRevisionBodyLength');
  151. }
  152. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  153. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  154. props._nextI18Next = nextI18NextConfig._nextI18Next;
  155. }
  156. function getAction(props: Props): SupportedActionType {
  157. let action: SupportedActionType;
  158. if (props.isExpired) {
  159. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  160. }
  161. else if (props.shareLink == null) {
  162. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  163. }
  164. else {
  165. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  166. }
  167. return action;
  168. }
  169. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  170. const req = context.req as CrowiRequest;
  171. const { crowi, params } = req;
  172. const result = await getServerSideCommonProps(context);
  173. if (!('props' in result)) {
  174. throw new Error('invalid getSSP result');
  175. }
  176. const props: Props = result.props as Props;
  177. try {
  178. const shareLink = await ShareLink.findOne({ _id: params.linkId }).populate('relatedPage');
  179. if (shareLink == null) {
  180. props.isNotFound = true;
  181. }
  182. else {
  183. props.isNotFound = false;
  184. props.isExpired = shareLink.isExpired();
  185. props.shareLink = shareLink.toObject();
  186. // retrieve Page
  187. const Page = crowi.model('Page');
  188. const relatedPage = await Page.findOne({ _id: getIdForRef(shareLink.relatedPage) });
  189. // determine whether skip SSR
  190. const ssrMaxRevisionBodyLength = crowi.configManager.getConfig('crowi', 'app:ssrMaxRevisionBodyLength');
  191. props.skipSSR = await skipSSR(relatedPage, ssrMaxRevisionBodyLength);
  192. // populate
  193. props.shareLinkRelatedPage = await relatedPage.populateDataToShowRevision(props.skipSSR); // shouldExcludeBody = skipSSR
  194. }
  195. }
  196. catch (err) {
  197. logger.error(err);
  198. }
  199. injectServerConfigurations(context, props);
  200. await injectNextI18NextConfigurations(context, props);
  201. await addActivity(context, getAction(props));
  202. return {
  203. props,
  204. };
  205. };
  206. export default SharedPage;